package org.helios.gwt.remote.sample.client.portlet;

import org.helios.gwt.remote.client.Exporter;
import org.helios.gwt.remote.sample.client.shared.PortalUtil;
import org.helios.gwt.remote.sample.client.shared.PortletHandle;
import org.helios.gwt.remote.sample.client.shared.PortletModuleHandle;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;

public class Portlet1EntryPoint implements EntryPoint
{
	public void onModuleLoad() 
	{
		PortalUtil.getPortal().registerPortletModule(new PortalModuleHandleImpl());
	}
	
	interface PortalModuleHandleExporter extends Exporter<PortletModuleHandle> {}
	
	public class PortalModuleHandleImpl implements PortletModuleHandle
	{
		public String getId() 
		{
			return "module1";
		}

		public String[] getPortletIds() {
			return new String[] {"portlet1"};
		}

		public PortletHandle placePortlet(String id, String whereId, String instanceId) 
		{
			// id = portlet1
			// instanceid = we don't need it in this example
			Portlet1 portlet = new Portlet1();
			RootPanel.get(whereId).add(portlet);
			return portlet.getHandle();
		}
	}
	
	interface PortletHandleExporter extends Exporter<PortletHandle> {}
	
	private class Portlet1 extends Composite
	{
		public Portlet1()
		{
			final Label label = new Label("portlet1"); 
			initWidget(label);
			label.addClickHandler(new ClickHandler() {
				
				public void onClick(ClickEvent event) {
					label.setText(label.getText() + "X");
				}
			});
			
			handle = new PortletHandleImpl();
		}
		
		private PortletHandle handle;
		
		public PortletHandle getHandle()
		{
			return handle;
		}
		
		private class PortletHandleImpl implements PortletHandle
		{
			public void removeFromParent() 
			{
				Portlet1.this.removeFromParent();
			}
		}
	}
}
